package com.bee.baselibrary.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;


import com.bee.baselibrary.R;
import com.bee.baselibrary.base.BaseActivity;
import com.bee.baselibrary.utils.ImageLoadUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.viewpager.widget.PagerAdapter;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


public class ImagePageAdapter extends PagerAdapter {

    private List<String> mlist = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;

    public ImagePageAdapter(Context context, List<String> mlist) {
        this.mlist = mlist;
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        if (null != mlist) {
            return mlist.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @SuppressLint("InflateParams")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = inflater.inflate(R.layout.photo_pager_item, null);
        PhotoView photoView = (PhotoView) view.findViewById(R.id.image);
        photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float v, float v1) {
                BaseActivity.context.finish();
            }

            @Override
            public void onOutsidePhotoTap() {

            }
        });
        ImageLoadUtils.ImageLoad(mlist.get(position),photoView,BaseActivity.context);
        // Now just add PhotoView to ViewPager and return it
        container.addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
